package com.vectz.autogen.action;

import java.io.IOException;
import java.util.ArrayList;

import com.vectz.autogen.model.Entity;
import com.vectz.autogen.model.EntityOverview;
import com.vectz.autogen.util.Constant;
import com.vectz.autogen.util.Log;
import com.vectz.autogen.util.Util;
/**
 * @author ai-t.vn
 *
 */
public class ModelCodegenAction {
	ArrayList<Entity> listField=null;
	EntityOverview table = null;
	String outputFolder = "";
	
	public ModelCodegenAction(ArrayList<Entity> listField, EntityOverview table, String outputFolder) {
		this.listField = listField;
		this.table = table;
		this.outputFolder = outputFolder;
		genernateModelFile();
	}
	/*
	 * Generate jsf file (add new entity form)
	 */
	public void genernateModelFile() {
		try {
			
			StringBuffer modelContent = new StringBuffer();
			
			String tmplFieldNameContent = Util.readTextFileContent(Util
					.getTmplPath("model","Entity-FieldName.txt"));
			String tmplFieldMethodContent = Util.readTextFileContent(Util
					.getTmplPath("model","Entity-FieldMethod.txt"));
			String tmplFileHeaderContent = Util.readTextFileContent(Util
					.getTmplPath("model","Entity-FileHeader.txt"));
			
			
			// add content path
			modelContent.append(genernateHeaderFile(table, tmplFileHeaderContent));
			modelContent.append(genernateFieldName(listField, tmplFieldNameContent));
			modelContent.append(genernateFieldMethod(listField, tmplFieldMethodContent));
			//append block end of class
			modelContent.append("}");
			String fileLocation = new StringBuffer().append(outputFolder)
							.append(System.getProperty("file.separator"))
							.append("src")
							.append(System.getProperty("file.separator"))							
							.append("model")
							.append(System.getProperty("file.separator"))
							.append(table.getName())
							.append(".java").toString();
			Util.writeJsfFile(fileLocation, modelContent.toString());
			Log.log("Finish generate model file to:" + fileLocation );
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	/*
	 * Generate header file
	 */
	public String genernateHeaderFile(EntityOverview dataTable, String tmpContent) {
		if (Util.isEmpty(tmpContent))
			return "";
		tmpContent = tmpContent.replaceAll("xxxxPackageName", dataTable
				.getPackageName());
		tmpContent = tmpContent
				.replaceAll("xxxxClassName", dataTable.getName());
	
//		tmpContent = tmpContent.replaceAll("xxxxEntityListeners", new StringBuilder().
//				append("@EntityListeners(").
//				append(dataTable.getEntityListenerClass()).
//				append(")").toString());
		
		return tmpContent;
	}

	/*
	 * Generate list field name
	 */
	public String genernateFieldName(ArrayList<Entity> listField,
			String tmplContent) {
		StringBuffer content = new StringBuffer();
		
		if (listField == null)
			return "";
		Entity field = null;
		for (int i = 0; i < listField.size(); i++) {
			field = (Entity) listField.get(i);
			String strAnnotation = "";
			if (field != null) {
				//replace field name
				String tmp = tmplContent.replaceAll("xxxxFieldName", field
						.getName());
				tmp = tmp.replaceAll("xxxxFieldDescription", field.getScreenName());
				//replace field type
				tmp = tmp.replaceAll("xxxxFieldType", Util
						.dbTypeToJavaType(field.getInputDataType()));
				//get @Id annotation
				if(field.getKeyType().equalsIgnoreCase("PK"))
					strAnnotation = "\t@Id\n";
				//get @NotNull annotation
				if(field.getRequired().equalsIgnoreCase("true"))
					strAnnotation += "\t@NotNull\n";
				//get @Length(max) annotation	
				if(!Util.isEmpty(field.getInputLength()))
					strAnnotation += new StringBuilder().
						append("\t@Column(length=").
						append(field.getInputLength()).
						append(")\n").toString();
				//replace /n in the end of annotation
				if(!Util.isEmpty(strAnnotation))
					strAnnotation = strAnnotation.substring(0,strAnnotation.length()-1);
				//replace all annotation
				tmp = tmp.replaceAll("xxxxFieldAnnotation",strAnnotation);
				content.append(tmp);
			}
		}

		return content.toString();
	}

	/*
	 * Generate list field method
	 */
	public String genernateFieldMethod(ArrayList<Entity> listField,
			String tmplContent) {
		StringBuffer content = new StringBuffer();
		if (listField == null)
			return "";
		Entity field = null;
		for (int i = 0; i < listField.size(); i++) {
			field = (Entity) listField.get(i);
			if (field != null) {
				String tmp = tmplContent.replaceAll("xxxxFieldType", Util
						.dbTypeToJavaType(field.getInputDataType()));
				String firstUpFieldName = Util.upperCaseFirstChar(field
						.getName());
				tmp = tmp.replaceAll("getXxxxFieldName", (new StringBuilder())
						.append("get").append(firstUpFieldName).toString());
				tmp = tmp.replaceAll("xxxxFieldName", field.getName());
				tmp = tmp.replaceAll("xxxxFieldDescription", field.getScreenName());
				// check if data type is java.lang.Boolean
				if (Util.dbTypeToJavaType(field.getInputDataType()).indexOf(
						"Boolean") != -1)
					tmp = tmp.replaceAll("setXxxxFieldName",
							(new StringBuilder()).append("is").append(
									firstUpFieldName).toString());
				else
					tmp = tmp.replaceAll("setXxxxFieldName",
							(new StringBuilder()).append("set").append(
									firstUpFieldName).toString());
				content.append(tmp);
			}
		}

		return content.toString();
	}
}
